<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          row-key="id"
          :tree-props="{children: 'details', hasChildren: 'hasChildren'}"
          :cell-style="cellStyle"
          :summary-method="getSummaries"
          :show-summary="true"
          :cell-class-name="cellClassName"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <!-- <el-table-column type="selection" align="center" width="70" :selectable="selectTable" /> -->
          <el-table-column type="selection" align="center" width="70" />
          <Column
            v-for="(column,index) in tableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
            :min-width="column.minWidth"
            :fixed="column.fixed"
            :align="column.align"
          />
          <el-table-column label="入库数量" align="right" fixed="right" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.inNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="出库数量" align="right" fixed="right" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.outNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="结存数量" align="right" fixed="right" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.balanceNum }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { storageList } from '@/api/warehouseManagement/storageModeling'
import { list, listInOutDetailsExport } from '@/api/warehouseManagement/materialDistributionDetails'
import { getDictSelects } from '@/api/common/common'
import ShowDetail from '@/components/JLINK2/common/ShowDetailTitle' // 查看页面
import { getInfo as enterWarehouseInfo } from '@/api/warehouseManagement/enterWarehouse' // 入库单
import { getInfo as elseEnterWarehouseInfo } from '@/api/warehouseManagement/elseEnterWarehouse' // 其他入库单
import { getInfo as outWarehouseInfo } from '@/api/warehouseManagement/outWarehouse'
import { getInfo as elseOutWarehouseInfo } from '@/api/warehouseManagement/elseOutWarehouse'

export default {
  name: 'Index',
  // components: { PrintMaterialInfo_large, PrintMaterialInfo_small }, // 组件注册
  data() {
    return {
      loading: true,
      clearable: false, // 日期选择器不可清除
      pageId: '物料收发明细', // 页面唯一标识
      pageExportId: '物料收发明细导出', // 页面导出标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        formTime: [],
        orderByColumn: 'materialCode',
        isAsc: 'asc'
      },
      pageData2: {
        pageNum: 1,
        pageSize: -1
      },
      totalWord: ['inNum', 'outNum'], // 合计导出字段
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'materialCode', label: '物料编码', minWidth: '160', canOrder: true, isRegular: true },
        { prop: 'materialName', label: '物料名称', minWidth: '160', canOrder: false, align: 'left', isRegular: true },
        { prop: 'materialModel', label: '规格型号', canOrder: false, isRegular: true },
        { prop: 'formCode', label: '单据编号', canOrder: false, minWidth: '160' },
        { prop: 'formTypeName', label: '单据类型', canOrder: false },
        { prop: 'formTimeStr', label: '单据时间', canOrder: false, isTime: true, minWidth: '180' },
        { prop: 'bindFormCode', label: '关联单据', canOrder: false, minWidth: '160' },
        { prop: 'warehouseName', label: '仓库', canOrder: false },
        { prop: 'remark', label: '备注', canOrder: false, align: 'left' },
        { prop: 'materialUnitName', label: '物料单位', canOrder: false }
        // { prop: 'inNum', label: '入库数量', canOrder:false, fixed: 'right' },
        // { prop: 'outNum', label: '出库数量', canOrder:false, fixed: 'right' },
        // { prop: 'balanceNum', label: '结存数量', canOrder:false, fixed: 'right' }
      ],
      defaultTableColumns2: [// 表格常规字段（初始值）
        { prop: 'materialCode', label: '物料编码', minWidth: '180', canOrder: true, position: 'left' },
        { prop: 'materialName', label: '物料名称', minWidth: '180', canOrder: true, position: 'left' },
        { prop: 'materialModel', label: '规格型号', canOrder: true, position: 'left' },
        { prop: 'formCode', label: '单据编号', canOrder: true },
        { prop: 'formTypeName', label: '单据类型', canOrder: true },
        { prop: 'formTimeStr', label: '单据时间', canOrder: true },
        { prop: 'bindFormCode', label: '关联单据', canOrder: true },
        { prop: 'warehouseName', label: '仓库', canOrder: true },
        { prop: 'remark', label: '备注', canOrder: true, position: 'left' },
        { prop: 'materialUnitName', label: '物料单位', canOrder: true },
        { prop: 'inNum', label: '入库数量', canOrder: true, align: 'right', position: 'right' },
        { prop: 'outNum', label: '出库数量', canOrder: true, align: 'right', position: 'right' },
        { prop: 'balanceNum', label: '结存数量', canOrder: true, align: 'right', position: 'right' }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'materialCode', label: '物料编码', minWidth: '180', canOrder: true, position: 'left' },
        { prop: 'materialName', label: '物料名称', minWidth: '180', canOrder: true, position: 'left' },
        { prop: 'materialModel', label: '规格型号', canOrder: true, position: 'left' },
        { prop: 'formCode', label: '单据编号', canOrder: true },
        { prop: 'formTypeName', label: '单据类型', canOrder: true },
        { prop: 'formTimeStr', label: '单据时间', canOrder: true },
        { prop: 'bindFormCode', label: '关联单据', canOrder: true },
        { prop: 'warehouseName', label: '仓库', canOrder: true },
        { prop: 'remark', label: '备注', canOrder: true, position: 'left' },
        { prop: 'materialUnitName', label: '物料单位', canOrder: true },
        { prop: 'inNum', label: '入库数量', canOrder: true, align: 'right', position: 'right' },
        { prop: 'outNum', label: '出库数量', canOrder: true, align: 'right', position: 'right' },
        { prop: 'balanceNum', label: '结存数量', canOrder: true, align: 'right', position: 'right' }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'materialCode', label: '物料编码', type: 'input' },
        // { prop: 'materialName', label: '物料名称', type: 'input' },
        // { prop: 'materialModel', label: '规格型号', type: 'input' },
        // { prop: 'formCode', label: '单据编号', type: 'input' },
        // { prop: 'formTypeName', label: '单据类型', type: 'input' },
        { prop: 'formTime', label: '单据时间', type: 'time', isTime: true, clearable: true, hasDefault: true, defaultKey: 'materTimeDefault' },
        // { prop: 'bindFormCode', label: '关联单据', type: 'input' },
        { prop: 'warehouseId', label: '仓库', type: 'select', optionsName: 'whatWarehouse' },
        { prop: 'materialUnitId', label: '物料单位', type: 'dict', optionsName: 'materialUnitOptions' }
      ],
      materTimeDefault: [], // 存放字段的默认值
      statusOptions2: [ // 选择组件数据源
        { value: undefined, label: '全部' },
        { value: 1, label: '启用' },
        { value: 0, label: '停用' }
      ],
      whatWarehouse: [],
      statusOptions: [],
      materialUnitOptions: [],
      descriptionOptions: [],
      Updata: [],
      operationData: [ // 右上角的操作按钮
        { label: '批量导出3', perm: '' },
        { label: '页面配置2', perm: '' }
      ],
      // 入库单
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '入库单号', key: 'formCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '入库人员', key: 'inByName' },
          { type: 'form', name: '入库日期', key: 'inTime' },
          { type: 'form', name: '入库类型', key: 'formTypeName' },
          { type: 'form', name: '关联单据', key: 'bindFormCode' }, // length:'double' 表示占两列
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '入库明细', key: 'details', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '入库数量', key: 'inStockNum', Width: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '批号', key: 'palletBatchCode', minWidth: '200' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '入库仓库', key: 'warehouseName', minWidth: '100' },
              { type: 'column', name: '入库库位', key: 'locationName', minWidth: '100' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '200' }
            ]
          }
        ]
      },
      // 其他入库单
      showKey2: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '其他入库单号', key: 'formCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '入库人员', key: 'inByName' },
          { type: 'form', name: '入库日期', key: 'inTime' },
          { type: 'form', name: '入库类型', key: 'formTypeName' },
          { type: 'form', name: '关联单据', key: 'bindFormCode' }, // length:'double' 表示占两列
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '入库明细', key: 'details', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '入库数量', fixed: 'right', key: 'inStockNum', minWidth: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '批号', key: 'palletBatchCode', minWidth: '150' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '入库仓库', fixed: 'right', key: 'warehouseName', minWidth: '100' },
              { type: 'column', name: '入库库位', fixed: 'right', key: 'locationName', minWidth: '100' },
              { type: 'column', name: '备注', key: 'remark', minWidth: '200' }
            ]
          }
        ]
      },
      // 出库单
      showKey3: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '出库单号', key: 'formCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '出库人员', key: 'outByName' },
          { type: 'form', name: '出库日期', key: 'outTime' },
          { type: 'form', name: '出库类型', key: 'formTypeName' },
          { type: 'form', name: '关联单据', key: 'bindFormCode' }, // length:'double' 表示占两列
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '出库明细', key: 'remarks', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '100' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '库存数量', key: 'stockNum', minWidth: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '待出库数量', key: 'waitOutNum', minWidth: this.SETTINGS.settings_decimalDigits_length }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '出库数量', key: 'outStockNumAll', minWidth: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', key: 'remark', name: '备注', minWidth: '200' }
            ]
          }
        ]
      },
      // 其他出库单
      showKey4: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '其他出库单号', key: 'formCode' }, // type: 'form' 表示是普通的表单展示
          { type: 'form', name: '出库人员', key: 'outByName' },
          { type: 'form', name: '出库日期', key: 'outTime' },
          { type: 'form', name: '出库类型', key: 'formTypeName' },
          { type: 'form', name: '关联单据', key: 'bindFormCode' }, // length:'double' 表示占两列
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
          { type: 'form', name: '备注', key: 'remark', length: 'double' },
          {
            type: 'table', name: '出库明细', key: 'remarks', columns: [ // type: 'table' 表示是表格
              { type: 'column', name: '物料编码', key: 'materialCode', minWidth: '120' }, // type: 'form' 表示是普通的表单展示
              { type: 'column', name: '物料名称', key: 'materialName', minWidth: '120' },
              { type: 'column', name: '规格型号', key: 'materialModel', minWidth: '100' },
              { type: 'column', name: '物料分类', key: 'materialTypeName', minWidth: '100' },
              { type: 'column', name: '物料单位', key: 'materialUnitName', minWidth: '100' },
              { type: 'column', name: '出库数量', fixed: 'right', key: 'outStockNumAll', minWidth: this.SETTINGS.settings_decimalDigits_length },
              { type: 'column', name: '备注', key: 'remark', minWidth: '200' }
            ]
          }
        ]
      }
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    this.getStorageList()
    this.getDictOptions()
    this.materTimeDefault = [this.$moment().startOf('month').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  methods: {
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    // 物料单位
    getDictOptions() {
      getDictSelects('materialUnitId').then(res => {
        this.materialUnitOptions = res.data
      })
      getDictSelects('descriptionId').then(res => {
        this.descriptionOptions = res.data
      })
    },
    // 仓库选择
    getStorageList() {
      storageList(this.formData).then(res => {
        res.data.forEach(item => {
          this.whatWarehouse.push({
            value: item.id,
            label: item.warehouseName
          })
        })
      })
    },
    cellStyle({ row, column }) {
      if (column.property == 'formCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'formCode':
          this.showDetail(row)
          break
      }
    },
    // selectTable(row, index) {
    //   if (row.formTypeName == '期初余额') {
    //     return false
    //   } else {
    //     return true
    //   }
    // },
    // 导出
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        listInOutDetailsExport(data).then(res => {
          resolve(res)
        })
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data.data
        res.data.data.forEach((item) => { item.isNotselected = true })
        this.total = res.data.total
        this.Updata = res
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
          // this.$refs.table.doLayout()
        })
      })
    },
    // 详情页
    showDetail(row) {
      if (row.formType == '入库') {
        enterWarehouseInfo(row.formId).then(res => {
          this.$layer.iframe({// 弹框
            content: {
              content: ShowDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                showKey: this.showKey
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看入库单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      } else if (row.formType == '其他入库') {
        elseEnterWarehouseInfo(row.formId).then(res => {
          this.$layer.iframe({// 弹框
            content: {
              content: ShowDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                showKey: this.showKey2
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看其他入库单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      } else if (row.formType == '出库') {
        outWarehouseInfo(row.formId).then(res => {
          this.$layer.iframe({// 弹框
            content: {
              content: ShowDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                showKey: this.showKey3
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看出库单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      } else if (row.formType == '其他出库') {
        console.log(row.formId)
        elseOutWarehouseInfo(row.formId).then(res => {
          this.$layer.iframe({// 弹框
            content: {
              content: ShowDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                showKey: this.showKey4
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看其他出库单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      }
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.label) {
          case '入库数量':
            sums[index] = this.Updata.inNums
            break
        }
        switch (column.label) {
          case '出库数量':
            sums[index] = this.Updata.outNums
            break
        }
      })
      console.log(sums)
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-table__fixed-right{
  height: 100% !important ;

}
/deep/.el-table__fixed-footer-wrapper tbody td.el-table__cell{
  border: none !important;
  background-color: transparent!important;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: rgba(0,0,0,0);
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  border-bottom: 1px solid #dfe6ec;
}

/deep/.el-table__fixed-footer-wrapper{
  bottom: 5px;
  padding-top: 10px;
  border-top: 1px solid #dfe6ec;
}
/deep/.el-table th.el-table__cell>.cell{
  padding-right: 20%;
}
/deep/.el-table .cell{
  padding-right: 20%;
}
/deep/.el-tooltip{
  display: flex;
}
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
</style>
